package com.famdoc.Service.Impl;

import com.famdoc.Dao.ArchiveMapper;
import com.famdoc.Pojo.dto.PatientArchiveForm;
import com.famdoc.Pojo.dto.Result;
import com.famdoc.Pojo.entity.Patient;
import com.famdoc.Service.ArchiveService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ArchiveServiceImpl implements ArchiveService {
    @Autowired
    private ArchiveMapper archiveMapper;

    /**
     * 建档
     * @param patientArchiveForm
     * @return
     */
    @Override
    public Result Filing(PatientArchiveForm patientArchiveForm) {
        Patient patient = archiveMapper.queryPatientByIdNumber(patientArchiveForm.getIdNumber());
        if(patient != null)
            return Result.success("该患者已存在！");
        //创建时间
        LocalDateTime localDateTime = LocalDateTime.now();
        patientArchiveForm.setCreateTime(localDateTime);
        Integer integer = archiveMapper.Filing(patientArchiveForm);
        if(integer==1)
            return Result.success();
        else
            return Result.error(201,"数据错误或不完整！");
    }

    /**
     * 分页查询个人档案（包括个人档案列表）
     * @param page
     * @param pageSize
     * @param name
     * @param idNumber
     * @param phoneNumber
     * @return
     */
    @Transactional
    @Override
    public Result QueryFile(Integer page, Integer pageSize, String name, String idNumber, String phoneNumber) {
        //开始分页查询
        PageHelper.startPage(page, pageSize);
        List<Patient> patientList =null;
        //查询
        patientList = archiveMapper.QueryFile(name, idNumber, phoneNumber);

        PageInfo pageInfo = new PageInfo(patientList);
        //封装数据
        Map<String, Object> patients = new HashMap<>();
        patients.put("patientList",pageInfo);

        //返回数据
        return Result.success(patients);
    }

    /**
     * 修改个人档案
     * @param patient
     * @return
     */
    @Override
    public Result modifyFile(Patient patient) {
        Integer integer=archiveMapper.modifyFile(patient);
        if(integer==1)
            return Result.success();
        else
            return Result.error(201,"修改失败，请重试！");
    }
}
